package com.detrum.sportcamera.fragment;

import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;

import com.detrum.sportcamera.R;
import com.detrum.sportcamera.adapter.CutDownAdapter;

public class CutDownFragment extends Fragment {

    private OnFragmentInteractionListener mListener;

    private GridView cutDown_gv;
    private CutDownAdapter cutDownAdapter;
    private int mParam1;
    private int flag = 5;


    public static CutDownFragment newInstance() {
        CutDownFragment fragment = new CutDownFragment();
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_cut_down, container, false);
        cutDown_gv = view.findViewById(R.id.cutDown_gv);
        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        cutDownAdapter = new CutDownAdapter(getActivity(),mParam1);
        cutDown_gv.setAdapter(cutDownAdapter);
        cutDown_gv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                mListener.selectItem(flag,position);
            }
        });
    }

    public void setParamsSuccess(int cuDownMode){
        cutDownAdapter.setPosition(cuDownMode);
    }

    public void setmParam1(int mParam1){
        this.mParam1 = mParam1;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof OnFragmentInteractionListener) {
            mListener = (OnFragmentInteractionListener) context;
        } else {
            throw new RuntimeException(context.toString()
                    + " must implement OnFragmentInteractionListener");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    /**
     * This interface must be implemented by activities that contain this
     * fragment to allow an interaction in this fragment to be communicated
     * to the activity and potentially other fragments contained in that
     * activity.
     * <p>
     * See the Android Training lesson <a href=
     * "http://developer.android.com/training/basics/fragments/communicating.html"
     * >Communicating with Other Fragments</a> for more information.
     */
    public interface OnFragmentInteractionListener {
        void onFragmentInteraction(Uri uri);
        void selectItem(int flag,int num);
    }
}
